//取值
var userInp = document.getElementById("user");
var userSpan = document.getElementById("user_span");

var pwdInp = document.getElementById("pwd");
var pwdSpan = document.getElementById("pwd_span");

var repwdInp = document.getElementById("repwd");
var repwdSpan = document.getElementById("repwd_span");

var codeInp = document.getElementById("code");
var createCodeSpan = document.getElementById("createCode");
var codeSpan = document.getElementById("code_span");

var btn = document.getElementById("btn");

var numArr = [];
for (var i = 48; i <= 57; i++) {
    var char = String.fromCharCode(i);
    numArr.push(char);
}
var bigArr = [];
for (var i = 65; i <= 90; i++) {
    var char = String.fromCharCode(i);
    bigArr.push(char);
}
var smallArr = [];
for (var i = 97; i <= 122; i++) {
    var char = String.fromCharCode(i);
    smallArr.push(char);
}
var speArr = ["_", "$"];
var lastArr = numArr.concat(bigArr, smallArr, speArr);
var yzArr = numArr.concat(bigArr, smallArr);
// console.log(numArr);

//初始验证码
// var chuArr = [];
// for (var i = 0; i < 4; i++) {
//     var index = Math.floor(Math.random() * yzArr.length);
//     var chuzhi = yzArr[index];
//     if (chuArr.indexOf(chuzhi) == -1) {
//         chuArr.push(chuzhi);
//     }
// }
// console.log(chuArr);

// var yzma;
// function createCode() {
//     var yzma = "";
//     var yzmaLength = 4;
//     var len_three = yzmaLength;
//     var chuArr = [];
//     for (var i = 0; i < len_three; i++) {
//         var index = Math.floor(Math.random() * yzArr.length);
//         var chuzhi = yzArr[index];
//         if (chuArr.indexOf(chuzhi) == -1) {
//             // chuArr.push(chuzhi);
//             yzma += chuzhi;
//             console.log(yzma);
//         }
//     }
//     checkCode.value = yzma;
// }
createCodeSpan.onclick = function () {
    var chuArr = [];
    var yazm = "";
    for (var i = 0; i < 4; i++) {
        var j = Math.floor(Math.random() * yzArr.length);
        var chuzhi = yzArr[j];
        if (chuArr.indexOf(chuzhi) == -1) {
            chuArr.push(chuzhi);
            yazm += chuzhi;
        }else{
            i--;
        }
    }
    
    console.log(chuArr);
    console.log(yazm);
    createCodeSpan.innerText = yazm;
}

btn.onclick = function () {

    //用户名
    var user = userInp.value;
    var len_one = user.length;

    if (len_one >= 6 && len_one <= 12) {
        var firstChar = user.charAt(0);
        if (numArr.indexOf(firstChar) == -1) {

            var flag = true;
            for (var i = 0; i < len_one; i++) {
                var char = user.charAt(i);
                if (lastArr.indexOf(char) == -1) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                userSpan.innerHTML = `<span  class="iconfont icon-duigou"></span>`;
                userSpan.style.color = "green";
            } else {
                userSpan.innerText = "* 用户名由数字 字母大小写 _ $ 组成";
                userSpan.style.color = "red";
                userSpan.style.fontSize = "12px";
            }
        } else {
            userSpan.innerText = "* 用户名不能以数字开头";
            userSpan.style.color = "red";
            userSpan.style.fontSize = "12px";
        }
    } else {
        userSpan.innerText = "* 用户名需要在6-12位之间";
        userSpan.style.color = "red";
        userSpan.style.fontSize = "12px";
    }



    //密码
    var pwd = pwdInp.value;
    var len = pwd.length;
    // console.log(len);
    if (len >= 6 && len <= 12) {
        var pasArr = [];
        for (var i = 0; i < len; i++) {
            var paschar = pwd.charAt(i);
            // console.log(paschar);

            if (numArr.indexOf(paschar) != -1) {
                var print = 1;
                pasArr.push(print);
            }

            if (bigArr.indexOf(paschar) != -1) {
                var print = 2;
                pasArr.push(print);
            }

            if (smallArr.indexOf(paschar) != -1) {
                var print = 3;
                pasArr.push(print);
            }

            if (speArr.indexOf(paschar) != -1) {
                var print = 4;
                pasArr.push(print);
            }


        }
        // console.log(pasArr);
        // console.log(pasArr.length);
        if (pasArr.length == len) {
            var qc = [];
            for (var i = 0; i < pasArr.length; i++) {
                var quzhi = pasArr[i];
                if (qc.indexOf(pasArr[i]) == -1) {
                    qc.push(quzhi);
                }
            }
        } else {
            pwdSpan.innerText = "* 密码由数字 大小写字母 _ 组成";
            pwdSpan.style.color = "red";
            pwdSpan.style.fontSize = "12px";
        }
        switch (qc.length) {
            case 1:
                pwdSpan.innerText = "弱";
                pwdSpan.style.color = "green";
                pwdSpan.style.fontSize = "12px";
                break;
            case 2:
                pwdSpan.innerText = "中";
                pwdSpan.style.color = "green";
                pwdSpan.style.fontSize = "12px";
                break;
            case 3:
                pwdSpan.innerText = "强";
                pwdSpan.style.color = "green";
                pwdSpan.style.fontSize = "12px";
                break;
            case 4:
                pwdSpan.innerText = "超强";
                pwdSpan.style.color = "green";
                pwdSpan.style.fontSize = "12px";
                break;
        }
    } else {
        pwdSpan.innerText = "* 密码需要在6-12位之间";
        pwdSpan.style.color = "red";
        pwdSpan.style.fontSize = "12px";
    }


    // 确认密码
    var repwd = repwdInp.value;
    // var len_two = repwd.length;

    // if (len_two == len) {
    //     var flag_two = true;
    //     debugger;
    //     for (var i = 0; i < len_two.length; i++) {
    //         if (repwd.charAt(i) !== pwd.charAt(i)) {
    //             flag_two = false;
    //             break;
    //         }
    //     }
    //     console.log(flag_two);
    //     if (flag_two) {
    //         repwdSpan.innerHTML = `<span  class="iconfont icon-duigou"></span>`;
    //         repwdSpan.style.color = "green";
    //     } else {
    //         repwdSpan.innerText = "* 密码不正确";
    //         repwdSpan.style.color = "red";
    //         repwdSpan.style.fontSize = "12px";
    //     }
    // } else {
    //     repwdSpan.innerText = "* 密码不正确";
    //     repwdSpan.style.color = "red";
    //     repwdSpan.style.fontSize = "12px";
    // }
    if (repwd == pwd) {
        repwdSpan.innerHTML = `<span  class="iconfont icon-duigou"></span>`;
        repwdSpan.style.color = "green";
    } else {
        repwdSpan.innerText = "* 密码不正确";
        repwdSpan.style.color = "red";
        repwdSpan.style.fontSize = "12px";
    }


    //确认验证码
    var code = codeInp.value;
    var yzma = createCodeSpan.value;
    console.log(yzma)

    if (code == yzma) {
        codeSpan.innerHTML = `<span  class="iconfont icon-duigou"></span>`;
        codeSpan.style.color = "green";
    } else {
        codeSpan.innerText = "* 验证码不正确";
        codeSpan.style.color = "red";
        codeSpan.style.fontSize = "12px";
    }
}